home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Transactor
/
Transactor_10_1986_Transactor_Publishing.d64
/
string calc
(
.txt
)
< prev
next >
Wrap
Commodore BASIC
|
2023-02-26
|
1KB
|
45 lines
10 printchr$(147)
20 input"enter formula";f$
30 iff$="stop"thenstop
40 a=0
50 f$=f$+ " ":p$=f$:id=9:dn=0
60 fori=1tolen(p$)
65 ifmid$(p$,i,1)=" "then80
70 next:print"?invalid formula":goto20
80 ifi=len(p$)thendn=1
90 x=i:x$=left$(p$,x)
100 va=1:v=val(x$)
105 ifval(x$)=0andleft$(x$,1)<>"0"thenva=0
110 ifleft$(x$,2)="pi"thenv=3.14159265:va=1
120 ifleft$(x$,1)="[255]"thenv=3.14159265:va=1
130 ifva=1then230
135 ifleft$(x$,1)="'"thenprint"st = ";a:goto310
140 id$=left$(x$,1):id=0
150 ifid$="+"thenid=1
160 ifid$="-"thenid=2
170 ifid$="*"thenid=3
180 ifid$="/"thenid=4
190 ifid$="^"thenid=5
195 if(id>0)and(id<90)andmid$(x$,2,1)<>" "thenprint"?error":goto20
200 ifid$=" "then310
203 ifa=0then210
204 ifid$="@"thenam=val(mid$(x$,2,1)):am=-am:goto207
205 if id$<>";"then210
206 am=val(mid$(x$,2,1)):ifa=0then210
207 a=int(a*(10^am)+.5):a=a/(10^am):id=0:goto310
210 ifid=0thenprint"?bad symbol":goto20
220 goto310
230 ifid=1thena=a+v
240 ifid=9thena=v:id=0:goto310
250 ifid=0thenprint"?error":(NULL)t20
260 ifid=2thena=a-v
270 ifid=3thena=a*v
280 ifid=5thena=a^v
290 ifid=4andv=0thenprint"?div by zero":goto20
300 ifid=4thena=a/v
310 ifdn=1then330
320 p$=right$(p$,(len(p$)-len(x$))):goto60
330 print"result = ";a:a=0:goto20
1000 rem notice this program is still subject to floating point
1010 rem round off errors and is only as accurate has basic permits